package com.toe.order.webapi.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.toe.common.restful.JsonResult;
import com.toe.order.service.OrderCartService;
import com.toe.pojo.dto.order.AddOrderCartDTO;
import com.toe.pojo.vo.order.OrderCartSimpleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/order-cart")
@Api(tags = "购物车模块")
public class OrderCartController {

    @Autowired
    private OrderCartService orderCartService;

    @PostMapping("/add")
    @ApiOperation("加入购物车")
    public JsonResult<Void> insert(@RequestBody AddOrderCartDTO addOrderCartDTO) {
        orderCartService.addNewCart(addOrderCartDTO);
        return JsonResult.ok();
    }

    @GetMapping("/delete")
    @ApiOperation("删除购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "30"),
            @ApiImplicitParam(value = "购物车id", name = "carId", required = true,
                    dataType = "long", example = "1"),
    })
    public JsonResult<List<OrderCartSimpleVO>> deleteById(Long userId, Long carId) {
        return JsonResult.ok(orderCartService.deleteById(userId, carId));
    }

    @GetMapping("/count-plus")
    @ApiOperation("购物车中的商品数量加一")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "30"),
            @ApiImplicitParam(value = "购物车id", name = "carId", required = true,
                    dataType = "long", example = "1"),
    })
    public JsonResult<List<OrderCartSimpleVO>> countPlusOneById(Long userId, Long carId) {
        return JsonResult.ok(orderCartService.countPlusOneById(userId, carId));
    }

    @GetMapping("/count-minus")
    @ApiOperation("购物车中的商品数量减一")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "30"),
            @ApiImplicitParam(value = "购物车id", name = "carId", required = true,
                    dataType = "long", example = "1"),
    })
    @SentinelResource("购物车商品数量减一")
    public JsonResult<Void> countMinusOneById(Long userId, Long carId) {
        orderCartService.countMinusOneById(userId, carId);
        return JsonResult.ok();
    }

    @GetMapping("/update-count")
    @ApiOperation("修改购物车中的商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "30"),
            @ApiImplicitParam(value = "购物车id", name = "carId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "购买数量", name = "newCount", required = true,
                    dataType = "int", example = "5"),
    })
    @SentinelResource("修改购物车商品数量")
    public JsonResult<List<OrderCartSimpleVO>> updateCountById(Long userId, Long carId, Integer newCount) {
        return JsonResult.ok(
                orderCartService.updateCountById(
                        userId,
                        carId,
                        newCount
                )
        );
    }

    @GetMapping("/update-specifications")
    @ApiOperation("修改购物车中的商品规格")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "30"),
            @ApiImplicitParam(value = "购物车id", name = "carId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "规格id", name = "specificationId",
                    required = true, dataType = "int", example = "5"),
    })
    public JsonResult<List<OrderCartSimpleVO>> updateSpecificationsById(Long userId, Long carId, Long specificationId) {
        return JsonResult.ok(
                orderCartService.updateSpecificationsById(
                        userId,
                        carId,
                        specificationId
                )
        );
    }

    @GetMapping("/simple")
    @ApiOperation("得到该用户的所有购物车商品信息")
    @ApiImplicitParam(value = "用户id", name = "userId",
                    required = true, dataType = "int", example = "30")
    public JsonResult<List<OrderCartSimpleVO>> getSimple(Long userId) {
        return JsonResult.ok(
                orderCartService.getOrderCartSimpleByUserId(userId)
        );
    }

    @GetMapping("/total-price")
    @ApiOperation("计算购物车已选商品的价格")
    public JsonResult<BigDecimal> getTolPrice(Long[] cartIds) {
        return JsonResult.ok(
                orderCartService.getTolPrice(cartIds));
    }

}

